/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import com.mycompany.psc.ejb.ReportableItemsManager;
import com.mycompany.psc.entities.Mobilization;
import com.mycompany.psc.entities.MobilizationItems;
import com.mycompany.psc.entities.Users;
import com.mycompany.psc.producers.LoggedIn;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author ahmed
 */
@Named
@SessionScoped
public class MobilizationController implements Serializable {

    @EJB
    ReportableItemsManager rm;
    @Inject
    ProjectsBean p;
     private String title;
     private String code;
    @Inject
    @LoggedIn
    Users current;
    private MobilizationItems newItem=new MobilizationItems();
    private Mobilization selectedResource;
    private MobilizationItems editableItem;

     public void save() {
       
        rm.saveMobilization( p.getSelectedReportProject(), current, getTitle(),code);
    }
     public void saveData(){
        getNewItem().setParent(getSelectedResource());
        getSelectedResource().getMobilizationItemsList().add(getNewItem());
        
        rm.update(getSelectedResource());
    }

    public MobilizationItems getEditableItem() {
        return editableItem;
    }

    public void setEditableItem(MobilizationItems editableItem) {
        this.editableItem = editableItem;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @return the newItem
     */
    public MobilizationItems getNewItem() {
        return newItem;
    }

    /**
     * @param newItem the newItem to set
     */
    public void setNewItem(MobilizationItems newItem) {
        this.newItem = newItem;
    }

    /**
     * @return the selectedResource
     */
    public Mobilization getSelectedResource() {
        return selectedResource;
    }

    /**
     * @param selectedResource the selectedResource to set
     */
    public void setSelectedResource(Mobilization selectedResource) {
        this.selectedResource = selectedResource;
    }
     public List<Mobilization> getMobilizationResources() {
        return rm.getMobilizationResources( p.getSelectedReportProject());
    }
     public List<MobilizationItems> getChildResources() {
        List<Mobilization> list = rm.getMobilizationResources( p.getSelectedReportProject());
        List<MobilizationItems> out=new ArrayList<>();
        for(Mobilization ti:list){
            out.addAll(ti.getMobilizationItemsList());
        }
        return out;
    }
 public void updateItem(){
        rm.update(selectedResource);
    }
    public void updateChildItem(){
        rm.update(editableItem);
    }
}
